<?php
    require_once('config.php');
    
    if(isset($_GET['article'])) {
        if(is_numeric($_GET['article'])) {
            
            if(isset($_POST['comment'])) {
                if(isset($_SESSION['isLogin'])) {
                    $com = $_POST['comment'];
                    $data = time();
                    $sql = 'INSERT INTO '.ARTICLES_COMMENT_TABLE.' VALUES(null, '.$userdata['id'].', '.$data.', :article_id, :text)';
                    $stmt = $pdo->prepare($sql);
                    $stmt->bindParam(':article_id', $_GET['article'], PDO::PARAM_INT);
                    $stmt->bindParam(':text', $com, PDO::PARAM_STR);
                    $stmt->execute();
                    $tpl->assign('script_info', debug::getMessage('Komentarz został dodany', 0));
                }
            }
            
include('includes/bbcode.class.php');
$bbcode = new bbcode;
            
            $id = $_GET['article'];
            $sql = 'SELECT a.*, u.login FROM articles a, user u WHERE a.id = :id AND a.user_id = u.id';
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':id', $id, PDO::PARAM_INT);
            $stmt->execute();
            if($stmt->rowCount()) {
                $result = $stmt->fetch(PDO::FETCH_ASSOC);
                if(isset($_SESSION['isLogin'])) {
                    $news_addComm = true;
                } else {
                    $site_config['notMemberComment'] ? $news_addComm = true : $news_addComm = false;
                }
                $tpl->assigns(Array(
                    'news_date' => $date->prepareDate($result['date'], true),
                    'news_title' => filterData($result['title'], 'string'),
                    'news_text' => $bbcode->parse(filterData($result['text'], 'string')),
                    'news_login' => $result['login'],
                    'news_link' => $result['id'].'-'.getNiceUrl($result['title']),
                    'news_addComm' => $news_addComm,
                ));
            } else {
                redirect('index.php', 1, 'news.php - no result');
            
            }
        } else {
           redirect('index.php', 1, 'news.php - not integer $_GET["article"]'); 
        }
    } else {
        redirect('index.php', 1, 'news.php - empty $_GET["article"]');
    }
    
    //Wyświetlenie szablonu
    $tpl->display('news_body.tpl');
?>